package com.qixin.modules.ea.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qixin.modules.ea.entity.EaAgencyReimbursement;
import com.qixin.modules.ea.mapper.EaAgencyReimbursementMapper;
import com.qixin.modules.ea.service.IEaAgencyReimbursementService;
import com.qixin.modules.ea.vo.EaAgencyReimbursementPageVo;
import com.qixin.modules.ea.vo.EaAgencyReimbursementVo;
import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaBasic.vo.IdVo;
import com.qixin.modules.eaBasic.vo.UserIdPageVo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DateFormatUtil;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.common.util.Uuid32Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: ea_agency_reimbursement
 * @Author: qixin-boot
 * @Date:   2021-09-22
 * @Version: V1.0
 */
@Service
public class EaAgencyReimbursementServiceImpl extends ServiceImpl<EaAgencyReimbursementMapper, EaAgencyReimbursement> implements IEaAgencyReimbursementService {
    @Autowired
    private EaAgencyReimbursementMapper eaAgencyReimbursementMapper;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    //代理设置列表-查询
    @Override
    public Map<String, Object> list(UserIdPageVo userIdPageVo) {
        // 获取当前登录用户
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String page = EmptyUtil.checkNull(userIdPageVo.getPage());    //页码
        String limit = EmptyUtil.checkNull(userIdPageVo.getLimit());    //页面大小
        String keyword = EmptyUtil.checkNull(userIdPageVo.getKeyword());    //关键字查询
        String sysOrgCode = EmptyUtil.checkNull(userIdPageVo.getSysOrgCode());    //所属组织pc

        String keyword1 = "%" + keyword + "%";
        userIdPageVo.setKeyword(keyword1);
        userIdPageVo.setPage((Integer.parseInt(page) -1) * Integer.parseInt(limit));
        userIdPageVo.setLimit(Integer.parseInt(limit));
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userid", loginUser.getId());
        map.put("keyword", userIdPageVo.getKeyword());
        map.put("limit", userIdPageVo.getLimit());
        map.put("page", userIdPageVo.getPage());
        map.put("sysOrgCode", sysOrgCode);
        List<EaAgencyReimbursement> eaAgencyReimbursementList = eaAgencyReimbursementMapper.selectEaAgencyReimbursementList(map);

//        int count = 0;
//        userIdPageVo.setPage(0);
//        userIdPageVo.setLimit(0);
//        Map<String, Object> map2 = new HashMap<String, Object>();
//        map2.put("userid", loginUser.getId());
//        map2.put("keyword", userIdPageVo.getKeyword());
//        map2.put("limit", userIdPageVo.getLimit());
//        map2.put("page", userIdPageVo.getPage());
//        map2.put("sysOrgCode", sysOrgCode);
//        List<EaAgencyReimbursement> eaAgencyReimbursementList2 = eaAgencyReimbursementMapper.selectEaAgencyReimbursementList(map2);
//        count = eaAgencyReimbursementList2.size();

        Map<String,Object> returnMap = new HashMap<String,Object>();
        returnMap.put("records",eaAgencyReimbursementList);
        returnMap.put("total",eaAgencyReimbursementList.size());
        return returnMap;
    }

    //新增代理设置
    @Override
    public Map<String, Object> add(EaAgencyReimbursementVo eaAgencyReimbursementVo) {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        //判断代理人是否已有代理设置
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("agent_id",eaAgencyReimbursementVo.getAgentId());   //代理人
        paramsMap.put("client_id",eaAgencyReimbursementVo.getClientId());   //委托人
        List<EaAgencyReimbursement> eaAgencyReimbursementList = eaAgencyReimbursementMapper.selectByMap(paramsMap);
        if(eaAgencyReimbursementList != null && eaAgencyReimbursementList.size() > 0){
            returnMap.put("code","-1");
            returnMap.put("message","委托人:" + changeIdToNameUtil.getNameById(eaAgencyReimbursementVo.getClientId()) +
                    "已设置代理人:" + changeIdToNameUtil.getNameById(eaAgencyReimbursementVo.getAgentId()) + ",不能重复设置!");
            return returnMap;
        }

        EaAgencyReimbursement eaAgencyReimbursement = new EaAgencyReimbursement();
        BeanUtils.copyProperties(eaAgencyReimbursementVo,eaAgencyReimbursement);    //将eaAgencyReimbursementVo实体值覆盖到eaAgencyReimbursement

        String uuid = Uuid32Util.getUUid();   //获取uuid
        eaAgencyReimbursement.setId(uuid);
        eaAgencyReimbursement.setCreateTime(new Date());
        eaAgencyReimbursementMapper.insert(eaAgencyReimbursement);

        returnMap.put("code","0");
        returnMap.put("message","设置成功!");
        return returnMap;
    }

    //编辑代理设置
    @Override
    public Map<String, Object> edit(EaAgencyReimbursementVo eaAgencyReimbursementVo) {
        Map<String,Object> returnMap = new HashMap<String,Object>();
        //判断代理人是否已有代理设置-去除自己
        Map<String,Object> paramsMap = new HashMap<String,Object>();
        paramsMap.put("agent_id",eaAgencyReimbursementVo.getAgentId());   //代理人
        paramsMap.put("client_id",eaAgencyReimbursementVo.getClientId());   //委托人
        List<EaAgencyReimbursement> eaAgencyReimbursementList = eaAgencyReimbursementMapper.selectByMap(paramsMap);
        if(eaAgencyReimbursementList != null && eaAgencyReimbursementList.size() > 0){

            Map<String,Object> paramsMap2 = new HashMap<String,Object>();
            paramsMap2.put("agent_id",eaAgencyReimbursementVo.getAgentId());   //代理人
            paramsMap2.put("client_id",eaAgencyReimbursementVo.getClientId());   //委托人
            paramsMap2.put("id",eaAgencyReimbursementVo.getId());   //id
            List<EaAgencyReimbursement> eaAgencyReimbursementList2 = eaAgencyReimbursementMapper.selectByMap(paramsMap2);
            if(eaAgencyReimbursementList2 != null && eaAgencyReimbursementList2.size() == 1){
                EaAgencyReimbursement eaAgencyReimbursement = new EaAgencyReimbursement();
                BeanUtils.copyProperties(eaAgencyReimbursementVo,eaAgencyReimbursement);    //将eaAgencyReimbursementVo实体值覆盖到eaAgencyReimbursement

                eaAgencyReimbursement.setUpdateTime(new Date());
                eaAgencyReimbursementMapper.updateById(eaAgencyReimbursement);

                returnMap.put("code","0");
                returnMap.put("message","设置成功!");
                return returnMap;
            }else{
                returnMap.put("code","-1");
                returnMap.put("message","委托人:" + changeIdToNameUtil.getNameById(eaAgencyReimbursementVo.getClientId()) +
                        "已设置代理人:" + changeIdToNameUtil.getNameById(eaAgencyReimbursementVo.getAgentId()) + ",不能重复设置!");
                return returnMap;
            }
        }else{
            EaAgencyReimbursement eaAgencyReimbursement = new EaAgencyReimbursement();
            BeanUtils.copyProperties(eaAgencyReimbursementVo,eaAgencyReimbursement);    //将eaAgencyReimbursementVo实体值覆盖到eaAgencyReimbursement

            eaAgencyReimbursement.setUpdateTime(new Date());
            eaAgencyReimbursementMapper.updateById(eaAgencyReimbursement);

            returnMap.put("code","0");
            returnMap.put("message","设置成功!");
            return returnMap;
        }
    }

    //删除代理设置
    @Override
    public void deleteById(IdVo idVo) {
        eaAgencyReimbursementMapper.deleteById(EmptyUtil.checkNull(idVo.getId()));
    }

    //查询代理设置详情
    @Override
    public Map<String, Object> selectById(IdVo idVo) {
        Map<String, Object> returnMap = new HashMap<String, Object>();

        EaAgencyReimbursementPageVo eaAgencyReimbursementPageVo = new EaAgencyReimbursementPageVo();    //实体数据存到eaAgencyReimbursementVo
        eaAgencyReimbursementPageVo.setPage(0);
        eaAgencyReimbursementPageVo.setLimit(0);
        eaAgencyReimbursementPageVo.setId(idVo.getId());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id",eaAgencyReimbursementPageVo.getId());
        map.put("keyword",eaAgencyReimbursementPageVo.getKeyword());
        map.put("limit",eaAgencyReimbursementPageVo.getLimit());
        map.put("page",eaAgencyReimbursementPageVo.getPage());

        List<EaAgencyReimbursement> eaAgencyReimbursementList = eaAgencyReimbursementMapper.selectEaAgencyReimbursementList(map);

        for (EaAgencyReimbursement eaAgencyReimbursement : eaAgencyReimbursementList) {
            returnMap = JSON.parseObject(JSON.toJSONString(eaAgencyReimbursement),Map.class);

            //格式化时间
            Long updateTime = returnMap.get("updateTime") != null ? Long.parseLong(returnMap.get("updateTime").toString()) : null;
            String updateTimeStr = DateFormatUtil.formatDateyyMMdd(updateTime);
            returnMap.put("updateTime",updateTimeStr);

            //格式化时间
            Long createTime2 = returnMap.get("createTime") != null ? Long.parseLong(returnMap.get("createTime").toString()) : null;
            String createTimeStr2 = DateFormatUtil.formatDateyyMMddHHmmss(createTime2);
            returnMap.put("createTime",createTimeStr2);
        }

        return returnMap;
    }


}
